package com.example.sofa_module.sofa.fragments.pics;

import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;


import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import com.example.baserecyclerview.quick.BaseQuickAdapter;
import com.example.common.mvvm.view.BaseFragment;
import com.example.sofa_module.R;
import com.example.sofa_module.databinding.FragmentSofachildBinding;
import com.example.sofa_module.sofa.entity.BeanEntity;


import java.util.List;

public class Fragment_child extends BaseFragment<FragmentSofachildBinding,ChildViewModel>implements BaseQuickAdapter.OnItemChildClickListener {

    private ChildAdapter childAdapter;

    @Override
    public View bindLayout() {
        return v.getRoot();
    }

    @Override
    public ViewBinding initBinding() {
        FragmentSofachildBinding fragmentSofachildBinding=FragmentSofachildBinding.inflate(inflater);
        return fragmentSofachildBinding;
    }
    @Override
    public void initView() {
        v.SofaRe.setLayoutManager(new LinearLayoutManager(getContext()));
        childAdapter = new ChildAdapter();
        childAdapter.setOnItemChildClickListener(this);
        v.SofaRe.setAdapter(childAdapter);
    }
    @Override
    public void initData() {
      vm.sofa_list.observe(this, new Observer<List<BeanEntity>>() {
          @Override
          public void onChanged(List<BeanEntity> beanEntities) {
              childAdapter.setNewInstance(beanEntities);
          }
      });
      vm.like_list.observe(this, new Observer<Boolean>() {
          @Override
          public void onChanged(Boolean aBoolean) {

                  //更新数据
                 childAdapter.notifyDataSetChanged();

          }
      });
    }

    @Override
    public void onResume() {
        super.onResume();
        vm.initData();
    }

    @Override
    public void onItemChildClick(RecyclerView.Adapter adapter, int id, int possition) {
        List<BeanEntity> data = childAdapter.getData();
        BeanEntity beanEntity = data.get(possition);
        int userId=beanEntity.author.userId;
        int conentId=beanEntity.id;
        if (id==R.id.item_video_likes_img){
            Toast.makeText(getContext(), "点赞", Toast.LENGTH_SHORT).show();
            vm.requestLike(conentId,userId);
            ImageView imageView=v.getRoot().findViewById(R.id.item_video_likes_img);
            imageView.setImageResource(com.example.widget_sdk.R.drawable.icon_cell_liked);
        }
    }

}
